import Dexie from "dexie";

export class LocalStorage {
  id?: number;
  value = "";

  constructor(value: string, id?: number) {
    this.value = value;
    if (id) this.id = id;
  }

  log() {
    console.log(JSON.stringify(this));
  }
}

export class AppDatabase extends Dexie {
  userInfo: Dexie.Table<LocalStorage, number> | undefined;
  userProperty: Dexie.Table<LocalStorage, number> | undefined;
  constructor() {
    super("EWG_DB");

    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const db = this;

    //
    // Define tables and indexes
    //
    db.version(1).stores({
      userInfo: "++id, value",
      userProperty: "++id, value"
    });

    // Let's physically map Contact class to contacts table.
    // This will make it possible to call loadEmailsAndPhones()
    // directly on retrieved database objects.
    this.userInfo?.mapToClass(LocalStorage);
    this.userProperty?.mapToClass(LocalStorage);
  }
}
export const db = new AppDatabase();
